package com.example.musicdemo;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import java.util.ArrayList;

/**
 * @类 MusicDemo
 * @作者: 吴金龙
 * @创建时间: 2022/2/25
 * @描述:
 */
public class SongUtils {
    public static ArrayList<Song> getAllSongs(Context context){
        ArrayList<Song> songs = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            Cursor cursor = context.getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    new String[]{
                          MediaStore.Audio.Media.TITLE,
                            MediaStore.Audio.Media.ARTIST,
                            MediaStore.Audio.Media.DATA
                    },null
                   ,null);

            songs = new ArrayList<>();
            if (cursor.moveToFirst()){
                Song song = null;
                do {
                    song = new Song();
                    song.setTitle(cursor.getString(0));
                    song.setSinger(cursor.getString(1));
                    song.setImage(cursor.getString(1));
                    song.setData(cursor.getString(2));
                    songs.add(song);
                }while (cursor.moveToNext());
                    cursor.close();

            }
        }

        return songs;
    }
}
